package main.java.com.lee.threadlocal;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description TODO
 * @Author winston
 * @DateTime 2021/12/16
 */
public class SpeedLog {
	private static final ThreadLocal<Map<String, Long>> TIME_RECORD_LOCAL = ThreadLocal.withInitial(SpeedLog::initialStartTime);

	private static Map<String, Long> initialStartTime() {
		HashMap<String, Long> map = new HashMap<>();
		map.put("start", System.currentTimeMillis());
		map.put("last", System.currentTimeMillis());
		return map;
	}

	public static void beginSpeedLog() {
		TIME_RECORD_LOCAL.get();
	}

	public static void endSpeedLog() {
		TIME_RECORD_LOCAL.remove();
	}

	public static void logPoint(String point) {
		Long start = TIME_RECORD_LOCAL.get().get("last");
		long cost = System.currentTimeMillis() - start;
		TIME_RECORD_LOCAL.get().put(point + "cost: ", cost);
		TIME_RECORD_LOCAL.get().put("last", System.currentTimeMillis());
	}

	public static void print() {
		System.out.println(TIME_RECORD_LOCAL.get().toString());
	}
}
